package com.g2room.myuall.grap.univ;

import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import com.g2room.myuall.grap.univ.po.ProvinceScorePo;
import com.g2room.myuall.grap.univ.to.AdminScoreTo;
import com.g2room.myuall.util.DbUtil;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.qiniu.util.Json;

public class GrapEolUnivMajocScore implements Runnable {
	// update univ_major_score_1 set md5=MD5(concat(schoolid,schoolname,specialtyname,localprovince,studenttype,year,batch,var,var_score,max,min));

	static String baseUrl = "http://gkcx.eol.cn";
	// 抓取各个专业  3146253
//	static String pageUrl = "http://data.api.gkcx.eol.cn/soudaxue/querySpecialtyScore.html?messtype=json&url_sign=querySpecialtyScore&size=50&page="; 
	static String pageUrl = "http://data.api.gkcx.eol.cn/soudaxue/querySpecialtyScore.html?messtype=json&url_sign=querySpecialtyScore&size=50&page="; 
	static Map<Integer, Boolean> grapedUrls = getGrapedUrl();
	static List<String> errList = new ArrayList<String>();
	static {}
	
	static int length = 854;
//	static int max = 62926;
	static int max = 10434;
	static int thread = 10;
	static int startPage = 1899;
	public static void main(String[] args) {
		for(int i =0; i< thread; i++) {
			GrapEolUnivMajocScore gue = new GrapEolUnivMajocScore();
			Thread t = new Thread(gue);
			t.start();
		}
	}
	@Override
	public void run() {
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		List<String> listUrls = getUrl();
		for(String url : listUrls) {
			getList(url);
		}
//			listUrls = getUrl();
	}
	
	public static AtomicInteger page = new AtomicInteger(startPage);
	private static synchronized List<String> getUrl() {
		if(page.intValue() > max) return null;
		List<String> listUrls = new ArrayList<String>();
		for(int i = page.intValue(); i< page.intValue() + length; i++) {
			if(i > max || (!grapedUrls.isEmpty()) && null != grapedUrls.get(i) && grapedUrls.get(i)) continue;
			listUrls.add(pageUrl + i);
			grapedUrls.put(i, true);
		}
		System.out.println(page.intValue() + " 开始，本次抓取：" + listUrls.size());
		page.addAndGet(length);
		return listUrls;
	}
	
	/**
	 * 获取分页列表数据
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public void getList(String url) {
		String body = null;
		try {
			body = new GenericMyuall().getJsonContent(url);
		} catch (Exception e1) {
			e1.printStackTrace();
//			insert2ErrDb(url.substring(url.indexOf("page=") + 5), 1);
			return;
		}
		if(null == body) return;
		try {
			body = body.replaceAll("\\[\\]", "\"\"");
			AdminScoreTo adminScoreTo = Json.decode(body, AdminScoreTo.class);
			insert2Db(adminScoreTo.getSchool());
			insert2GrapedDb(url.substring(url.indexOf("page=") + 5));
		} catch (Exception e1) {
			e1.printStackTrace();
//			insert2ErrDb(url.substring(url.indexOf("page=") + 5), 2);
			return;
		}
	}
	
	
	public void insert2Db(List<ProvinceScorePo> pss) {
		try {
			Connection conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			PreparedStatement univPs = conn.prepareStatement("INSERT INTO `univ_major_score_1` ("
					+ "province_name,year,batch_name,old_school_id,school_name,student_type_name,var,var_score,max,min,major_name,url) "
					+ "values(?,?,?,?,?,?,?,?,?,?,?,?)");
			for(ProvinceScorePo as : pss) {
				univPs.setString(1, as.getLocalprovince());
				univPs.setString(2, as.getYear());
				univPs.setString(3, as.getBatch());
				univPs.setInt(4, as.getSchoolid());
				univPs.setString(5, as.getSchoolname());
				univPs.setString(6, as.getStudenttype());
				univPs.setString(7, as.getVar());
				univPs.setString(8, as.getVar_score());
				univPs.setString(9, as.getMax());
				univPs.setString(10, as.getMin());
				univPs.setString(11, as.getSpecialtyname());
				univPs.setString(12, as.getUrl());
				univPs.addBatch();
			}
			univPs.executeBatch();
			conn.commit();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void insert2GrapedDb(String url) {
		try {
			PreparedStatement grapPs = DbUtil.getConn().prepareStatement("INSERT INTO `univ_graped_1` (`url`) "
					+ "values(?)");
			grapPs.setInt(1, Integer.valueOf(url));
			grapPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static Map<Integer, Boolean> getGrapedUrl() {
		Map<Integer, Boolean> its = new HashMap<Integer, Boolean>();
		try {
			Connection connection = DbUtil.getConn();
			Statement statement = connection.createStatement();
			String sql = "select url from univ_graped_1";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				its.put(rs.getInt("url"), true);
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return its;
	}

}